package org.dromara.iot.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.iot.domain.bo.IotAnnualPlanBo;
import org.dromara.iot.domain.vo.IotAnnualPlanVo;
import org.dromara.iot.service.IIotAnnualPlanService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电梯年检计划
 *
 * @author lsl
 * @date 2024-06-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/iot/annualPlan")
public class IotAnnualPlanController extends BaseController {

    private final IIotAnnualPlanService iotAnnualPlanService;

    /**
     * 查询电梯年检计划列表
     */
    @SaCheckPermission("iot:annualPlan:list")
    @GetMapping("/list")
    public TableDataInfo<IotAnnualPlanVo> list(IotAnnualPlanBo bo, PageQuery pageQuery) {
        return iotAnnualPlanService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出电梯年检计划列表
     */
    @SaCheckPermission("iot:annualPlan:export")
    @Log(title = "电梯年检计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(IotAnnualPlanBo bo, HttpServletResponse response) {
        List<IotAnnualPlanVo> list = iotAnnualPlanService.queryList(bo);
        ExcelUtil.exportExcel(list, "电梯年检计划", IotAnnualPlanVo.class, response);
    }

    /**
     * 获取电梯年检计划详细信息
     *
     * @param planId 主键
     */
    @SaCheckPermission("iot:annualPlan:query")
    @GetMapping("/{planId}")
    public R<IotAnnualPlanVo> getInfo(@NotNull(message = "主键不能为空")
                                      @PathVariable Long planId) {
        return R.ok(iotAnnualPlanService.queryById(planId));
    }

    /**
     * 新增电梯年检计划
     */
    @SaCheckPermission("iot:annualPlan:add")
    @Log(title = "电梯年检计划", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody IotAnnualPlanBo bo) {
        return toAjax(iotAnnualPlanService.insertByBo(bo));
    }

    /**
     * 修改电梯年检计划
     */
    @SaCheckPermission("iot:annualPlan:edit")
    @Log(title = "电梯年检计划", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody IotAnnualPlanBo bo) {
        return toAjax(iotAnnualPlanService.updateByBo(bo));
    }

    /**
     * 删除电梯年检计划
     *
     * @param planIds 主键串
     */
    @SaCheckPermission("iot:annualPlan:remove")
    @Log(title = "电梯年检计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{planIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] planIds) {
        return toAjax(iotAnnualPlanService.deleteWithValidByIds(List.of(planIds), true));
    }

    /**
     * 一键添加年检计划
     */
    @SaCheckPermission("iot:annualPlan:add")
    @Log(title = "电梯年检计划", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/generate")
    public R<Void> generate() {
        return toAjax(iotAnnualPlanService.generate());
    }
}

